package com.ruoyi.operation.order.data.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.operation.order.data.domain.OrderData;
import com.ruoyi.operation.order.data.service.IOrderDataService;
import com.ruoyi.operation.order.wwebchat.domain.Wwebchat;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author wangzhiwei phone(WeChat)):15062775766
 * @date 2020/5/2 21:22
 */
@Controller
@RequestMapping(value = "/data/data")
public class OrderDataController extends BaseController {

    // 定义一个跳转到页面的路径
    private String prefix = "order/data/";

    @Autowired
    IOrderDataService iOrderDataService;

    /**
     * 跳转到数据录入的页面
     * @return
     */
    @RequiresPermissions(value = "data:data:data")
    @GetMapping()
    String data(){
        return prefix + "data";
    }

    /**
     * 分页查询所有的数据
     * @param orderData
     * @return
     */
    @RequestMapping(value = "/list")
    @RequiresPermissions(value = "data:data:data")
    @ResponseBody
    public TableDataInfo findAllList(OrderData orderData){
        startPage();
        List<OrderData> orderDataList = iOrderDataService.findAll(orderData);
        return getDataTable(orderDataList);

    }

    /**
     * 导入信息
     * @param file
     * @return
     * @throws Exception
     */
//    @Log(title = "数据模块", businessType = BusinessType.IMPORT)
//    @RequiresPermissions("data:data:import")
//    @PostMapping("/importData")
//    @ResponseBody
//    public AjaxResult importData(@RequestParam("file")MultipartFile file) throws Exception{
//
//        iOrderDataService.importExcel(file);
//        String message = "数据导入成功！";
//        return AjaxResult.success(message);
//    }

    /**
     * 导出Excel表格
     * @param orderData
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/exportExcel")
    public void exportExcel(OrderData orderData, HttpServletRequest request, HttpServletResponse response) throws Exception{
        iOrderDataService.exportExcel(orderData,request,response);
    }


    @RequestMapping(value = "/countNum")
    @ResponseBody
    public AjaxResult countWw(String wwNum){
        return AjaxResult.success(iOrderDataService.countWwNum(wwNum)+"");
    }

    /**
     * 批量删除客户信息
     * @param dataIds
     * @return
     */
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String dataIds)
    {
        try
        {
            return toAjax(iOrderDataService.batchDeleteIds(dataIds));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    /**
     * 导入信息
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "录入管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("data:data:import")
    @PostMapping("importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {

        ExcelUtil<OrderData> util = new ExcelUtil<OrderData>(OrderData.class);
        List<OrderData> orderDataList = util.importExcel(file.getInputStream());
        String message = iOrderDataService.importOrderData(orderDataList, updateSupport);
        return AjaxResult.success(message);
    }
}
